package cn.tedu.csmall.product.controller;

import cn.tedu.csmall.product.pojo.dto.AttributeAddNewDTO;
import cn.tedu.csmall.product.pojo.vo.AttributeListItemVO;
import cn.tedu.csmall.product.service.AttributeService;
import cn.tedu.csmall.product.web.JsonResult;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Slf4j
@RestController
@RequestMapping("/attributes")
@Api(tags = "04. 属性管理模块")
@Validated
public class AttributeController {

    @Autowired
    private AttributeService attributeService;

    public AttributeController() {
        log.info("创建控制器：AttributeController");
    }

    // http://localhost:8080/attributes/add-new?
    @ApiOperation("添加属性")
    @ApiOperationSupport(order = 100)
    @PostMapping(value = "/add-new")
    public JsonResult addNew(@RequestBody AttributeAddNewDTO attributeAddNewDTO) {
        log.debug("开始处理【添加属性】的请求，参数：{}", attributeAddNewDTO);
        attributeService.addNew(attributeAddNewDTO);
        return JsonResult.ok();
    }

    // http://localhost:9080/attributes/9527/delete
    @ApiOperation("删除属性")
    @ApiOperationSupport(order = 200)
    @ApiImplicitParam(name = "id", value = "属性id", required = true, dataType = "long")
    @PostMapping("/{id:[0-9]+}/delete")
    public JsonResult<Void> delete(@PathVariable Long id) {
        log.debug("开始处理【删除属性】的请求：id={}", id);
        attributeService.delete(id);
        return JsonResult.ok();
    }

    // http://localhost:9080/attributes
    @ApiOperation("查询属性列表")
    @ApiOperationSupport(order = 410)
    @GetMapping("")
    public JsonResult<List<AttributeListItemVO>> list() {
        log.debug("开始处理【查询属性列表】的请求……");
        List<AttributeListItemVO> list = attributeService.list();
        return JsonResult.ok(list);
    }
}
